//
//  MANoteReplyCell.m
//  meiang_edu
//
//  Created by 李国栋 on 2018/10/17.
//  Copyright © 2018年 com.meiang.meiang_edu. All rights reserved.
//

#import "MANoteReplyCell.h"
#import "CollectionHShowView.h"
#import "MANoteImageModel.h"
@interface MANoteReplyCell()

@property (weak, nonatomic) IBOutlet UIImageView *imgHeader;
@property (weak, nonatomic) IBOutlet UILabel *lblName;

@property (weak, nonatomic) IBOutlet UILabel *lblParent;
@property (weak, nonatomic) IBOutlet UILabel *lblContent;

/**
 图片容器
 */
@property (weak, nonatomic) IBOutlet UIView *viewImageContent;

/**
 图片展示器,collectionView
 */
@property (weak, nonatomic) CollectionHShowView * viewImageShow;

@property (weak, nonatomic) IBOutlet UILabel *lblTime;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *layoutViewImgsHeight;


@property (nonatomic, strong) MANoteReplyModel *replyModel;
@property (nonatomic, strong) NSIndexPath *indexPath;
@end

@implementation MANoteReplyCell

- (void)setDataWithModel:(MANoteReplyModel *)model indexPath:(NSIndexPath *)indexPath delegate:(id<MANoteReplyCellProtocol>)delegate{
    _replyModel = model;
    _indexPath = indexPath;
    _delegate = delegate;
    [self.imgHeader ma_setImageWithURL:model.sender_img placeholderImage:[UIImage imageNamed:@"picture_logo"]];
    self.lblName.text = model.sender_name;
    self.lblParent.text = model.sender_username;
    self.lblContent.text = model.message_content;
    self.lblTime.text = model.send_time;
    
    NSArray * arrayImgUrl = [model.message_images componentsSeparatedByString:@","];
    UIImage *placeholderImage = [UIImage imageNamed:@"picture_logo"];
    NSMutableArray * arrayImageModels = [NSMutableArray array];
    for (NSString * url in arrayImgUrl) {
        MANoteImageModel * imgModel = [MANoteImageModel new];
        imgModel.url = url;
        imgModel.placeholderImage = placeholderImage;
        [arrayImageModels addObject:imgModel];
    }
    [_viewImageShow setDataSource:arrayImageModels];
    
    NSInteger rowCount = arrayImgUrl.count/3;
    if ((arrayImgUrl.count%3) != 0) {
        rowCount += 1;
    }
    //图片区域高度
    CGFloat imageContentHeight = [self getImageSize].height * rowCount + (rowCount>0?(4*(rowCount-1)):0);
    self.layoutViewImgsHeight.constant = imageContentHeight;
}

- (CGFloat)cellHeightWithModel:(MANoteReplyModel*)model{
    self.lblContent.text = model.message_content;
    CGFloat height = [self.lblContent   sizeThatFits:CGSizeMake(SCREEN_WIDTH - 12*2, MAXFLOAT)].height;
    //    计算图片区域高度
    NSArray * arrayImgUrl = [model.message_images componentsSeparatedByString:@","];
    NSInteger rowCount = arrayImgUrl.count/3;
    if ((arrayImgUrl.count%3) != 0) {
        rowCount += 1;
    }
    CGFloat imageContentHeight = [self getImageSize].height * rowCount + (rowCount>0?(4*(rowCount-1)):0);
    height += imageContentHeight;
    return height + 134 - 20;
}
- (IBAction)tapDelete:(id)sender {
    if ([self.delegate respondsToSelector:@selector(msgReplyCellDidTapDelete:indexPath:)]) {
        [self.delegate msgReplyCellDidTapDelete:self indexPath:_indexPath];
    }
}

- (CGSize)getImageSize{
    CGFloat imageWidth = (SCREEN_WIDTH - 4*2-12*2-4*2)/3.0;
    CGFloat imageHeight = imageWidth * (68.0/108);
    return CGSizeMake(imageWidth, imageHeight);
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = [self getImageSize];
    layout.minimumLineSpacing = 4;
    layout.minimumInteritemSpacing = 0;
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    
    CollectionHShowView * hView = [[CollectionHShowView alloc] initWithNibCell:@"MANoteImageCell" modelName:@"MANoteImageModel" cellSize:[self getImageSize] layout:layout];
    [_viewImageContent addSubview:hView];
    [hView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.mas_equalTo(0);
        make.right.bottom.mas_equalTo(0);
    }];
    hView.collectionView.scrollEnabled = NO;
    _viewImageShow = hView;
    _viewImageShow.clipsToBounds = YES;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}

@end
